﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.CMS263.Util;
using Com.CMS263.PO;
using Com.CMS263.BLL;
using System.IO;
namespace Com.CMS263.Web.manage.ProductManage
{
    public partial class product_add : System.Web.UI.Page
    {
        ProductBLL pbll = new ProductBLL();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string title = StringUtil.checkStr(Request["txtTitle"]);
            string productNum = StringUtil.checkStr(Request["txtProductNum"]);
            string description = StringUtil.checkStr(Request["txtDescription"]);
            string style = StringUtil.checkStr(Request["style"]);
            string ispub = StringUtil.checkStr(Request["state"]);
            string type = StringUtil.checkStr(Request["txtType"]);
            string salePrice = StringUtil.checkStr(Request["txtSalePrice"]);

            HttpPostedFile file = Request.Files["txtProductImg"];
            if (string.IsNullOrEmpty(title))
            {
                StringUtil.Alert("名称不能为空", "-1");
            }
            if (string.IsNullOrEmpty(productNum))
            {
                StringUtil.Alert("编号不能为空", "-1");
            }
            if (string.IsNullOrEmpty(description))
            {
                StringUtil.Alert("简介不能为空", "-1");
            }
            if (string.IsNullOrEmpty(style))
            {
                StringUtil.Alert("请选择所属档次", "-1");
            }
            if (string.IsNullOrEmpty(salePrice))
            {
                StringUtil.Alert("请输入产品价格", "-1");
            }
            if (file.ContentLength < 5)
            {
                StringUtil.Alert("请上传图片", "-1");
            }
            //文件按天存储，没有目录就自己创建
            string strDay = DateTime.Now.ToString("yyyy-MM-dd");
            string savePath = Server.MapPath("/ArtUpFile/" + strDay);
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }
            Product prod = new Product();
            prod.IsPub = Convert.ToBoolean(Convert.ToInt32(ispub));
            prod.ProductDescription = description;
            prod.ProductImg =strDay+"/"+ Util.UpLoadFileUtil.UpLoadFile(file, savePath);
            prod.ProductNum = productNum;
            prod.SortId = pbll.GetMaxId();
            prod.StyleIds = style;
            prod.Title = title;
            prod.SalePrice = salePrice;
            prod.ProductType = Convert.ToInt32(type);
            pbll.entity = prod;
            if (pbll.Add())
            {
                StringUtil.Alert("添加成功", "product_list.aspx");
            }
            
            
        }

    }
}
